// pages/my/index.js
var requestService = require('../../utils/requestService.js');
var qrCodeUtil = require('../../utils/qrCodeUtil.js');
Page({
    data: {
        stationId:'',/**站点编号*/
        stationInfo:{},/**站点信息*/
        chargers:[]/**充电桩列表*/
    },
    // 页面加载
    onLoad: function (option) {

        const that=this;
        // 设置本页导航标题
        wx.setNavigationBarTitle({
            title: '设备详情'
        })
        that.setData({
            stationId:option.stationId,
            "stationInfo.stationId" : option.stationId,
            "stationInfo.stationName" : option.stationName,
            "stationInfo.parkingAddress" : option.parkingAddress,
            "stationInfo.distance" : option.distance
        })

        // 请求服务器，查询该站点的充电桩列表
        requestService.sendGetRequest("/station/chargers/"+this.data.stationId,null,function(result){
            var chargers=result.data.resData;
            that.setData({
                chargers:chargers
            })
        },function(){
            wx.showModal({
                title: '提示',
                content: '获取站点充电桩信息异常',
                showCancel: false
            })
        })
    },
    /**充电预约*/
    chargeReservation:function(event){
        if(true){
            wx.showModal({
                title: '提示',
                content: '暂不支持预约',
                showCancel: false
            })
        }else{
            const chargerId=event.currentTarget.id;
            requestService.sendPostRequest("/charger/chargeReservation",{'chargerId':chargerId,portNo:'1'},function(result){
                const resultData=result.data;
                if(resultData.resCode==0){
                    wx.showModal({
                        title: '提示',
                        content: '预约成功',
                        showCancel: false
                    })
                }else{
                    wx.showModal({
                        title: '提示',
                        content: resultData.message,
                        showCancel: false
                    })
                }

                console.log(result);
            },function(result){
                wx.showModal({
                    title: '提示',
                    content: '发送充电预约请求失败',
                    showCancel: false
                })
            })
        }
    },
    /**扫码充电*/
    scanCharge : function (controlId){
        const that=this;
        requestService.sendGetRequest("/charger/userChargState",null,function(result){
            const chargerProgress=result.data.resData;
            if(chargerProgress!=null && (chargerProgress.chargeStatus=="1" || chargerProgress.chargeStatus=="2")){
                // 当前正在充电或已预约跳转到计费页面
                wx.navigateTo({
                    url: '../charge/index?address='+chargerProgress.address
                })
            }else{
                // 没有在计费就扫码
                wx.scanCode({
                    scanType: "qrCode",
                    success: (res) => {
                    // 请求服务器开起充电
                    let qrCodeRes=res.result;

                let qrCode=qrCodeUtil.decompose(res.result);
                console.log(qrCode);
                if(qrCode){
                    wx.navigateTo({
                        url: '../charge/prepare?qrCode='+qrCode
                    })
                }else{
                    wx.showModal({
                        title: '提示',
                        content: '扫描二维码失败',
                        showCancel: false
                    })
                }
            },
                fail: function() {
                    wx.showModal({
                        title: '提示',
                        content: '扫描二维码失败',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                console.log('用户点击确定')
                            }
                        }
                        })
                    }
                })
                }
            });

    },
    /**发送充电请求*/
    sendChargeRequest: function (qrCode){
        requestService.sendPostRequest("/charger/scanCharge",{'qrCode':qrCode},function(result){
            const resultData=result.data;
            if(resultData.resCode==0){
                wx.showModal({
                    title: '提示',
                    content: '正在充电…………',
                    showCancel: false
                })
            }else{
                wx.showModal({
                    title: '提示',
                    content: resultData.message,
                    showCancel: false
                })
            }

            console.log(result);
        },function(result){
            wx.showModal({
                title: '提示',
                content: '发送充电请求失败',
                showCancel: false
            })
        })
    }
})